package com.shg.web.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.shg.bean.Author;
import com.shg.bean.PageView;
import com.shg.service.AuthorService;
import com.shg.web.tools.BaseConstrant;
import com.shg.web.tools.FreeMarkerTool;
import com.shg.web.tools.WebTool;

@Controller
public class AuthorAction {

	//最红作者
	private List<Author> bestAuthor;
	//最新作者
	private List<Author> newAuthor;
	//作者排列帮
	private List<Author> authorArray;
	//分页查询所有的作者
	private PageView<Author> pageView;
	//
	private int maxrecord=3;
	//
	private int currentpage=1;
	
	private Map<String, Object>  authorMap;
	//模板引擎的文件名称
	private String ftlName;
	//模板引擎生成的静态页面名称
	private String htmlName;
	private String message;
	//作者
	private Author author;
	
	@Resource AuthorService authorService;
	
	/**
	 * 最红作者
	 * @return
	 */
	public String  bestAuthor(){
		pageView=new PageView<Author>(maxrecord, currentpage);
		try {
			pageView.setQueryResult(authorService.getBestAuthor(pageView));
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return "bestAuthor";
	}
	/**
	 * 生成静态HTML
	 * @return
	 */
	public String bestAuthorMap(){
		try {
			authorMap = authorService.getBestAuthor();
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		FreeMarkerTool.analysisTemplate(BaseConstrant.FTLPATH, ftlName, BaseConstrant.HTMLPATH,htmlName, authorMap);
		return "bestAuthorMap";
	}
	/**
	 * 最新作者
	 */
	public String newAuthor(){
		try {
			newAuthor=authorService.getNewAuthor(10);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return "newAuthor";
	}
	/**
	 * 最后欢迎的作者
	 * @return
	 */
	public String love(){
		try {
			authorArray = authorService.getLove(10);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return "love";
	}
	/**
	 * 作者注册
	 */
	public String authorregister(){
		try {
			author = (Author) authorService.select(Author.class, WebTool.getLogin().getId());
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		if(author==null){
			return "authorregister";
		}else{
			return "updateAuthor";
		}
	}
	/**
	 * 作者注册
	 * @return
	 */
	public String register(){
		try {
			author.setUsers(WebTool.getLogin());
			authorService.insert(author);
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
		return "success";
	}
	public List<Author> getBestAuthor() {
		return bestAuthor;
	}
	public void setBestAuthor(List<Author> bestAuthor) {
		this.bestAuthor = bestAuthor;
	}
	public List<Author> getNewAuthor() {
		return newAuthor;
	}
	public void setNewAuthor(List<Author> newAuthor) {
		this.newAuthor = newAuthor;
	}
	public List<Author> getAuthorArray() {
		return authorArray;
	}
	public void setAuthorArray(List<Author> authorArray) {
		this.authorArray = authorArray;
	}
	public Map<String, Object> getAuthorMap() {
		return authorMap;
	}
	public void setAuthorMap(Map<String, Object> authorMap) {
		this.authorMap = authorMap;
	}
	public PageView<Author> getPageView() {
		return pageView;
	}
	public void setPageView(PageView<Author> pageView) {
		this.pageView = pageView;
	}
	public int getMaxrecord() {
		return maxrecord;
	}
	public void setMaxrecord(int maxrecord) {
		this.maxrecord = maxrecord;
	}
	public int getCurrentpage() {
		return currentpage;
	}
	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}
	public String getFtlName() {
		return ftlName;
	}
	public void setFtlName(String ftlName) {
		this.ftlName = ftlName;
	}
	public String getHtmlName() {
		return htmlName;
	}
	public void setHtmlName(String htmlName) {
		this.htmlName = htmlName;
	}
	public Author getAuthor() {
		return author;
	}
	public void setAuthor(Author author) {
		this.author = author;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	
	
}
